<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>尊敬的管理员请登录</span>
      </div>
      <div class="text item">
        <el-form
          label-position="left"
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="1.2rem"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="userName">
            <el-input
              type="userName"
              v-model="ruleForm.userName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm('ruleForm')"
              :loading="loginbtn"
              >登录</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { post } from "@/utils/axiosTools";
import { setToken } from "@/utils/TokenTools";
export default {
  name: "Login",
  data() {
    var validateUsername = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        userName: "",
      },
      rules: {
        userName: [{ validator: validateUsername, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
      },
      loginbtn: false,
    };
  },

  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          this.loginbtn = true;
          post("/api/v1/auth/manager_login", {
            userName: this.ruleForm.userName,
            password: this.ruleForm.pass,
          })
            .then((result) => {
              console.log(result);
              if (result.code == 1) {
                console.log(result);
                this.$message({
                  type: "success",
                  message: result.info,
                });
                setToken(result.token);
                this.$router.push({ name: "Home" });
              } else {
                this.$message({
                  type: "warning",
                  message: result.info,
                });
              }
              this.loginbtn = false;
            })
            .catch((err) => {
              this.loginbtn = false;
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.box-card {
  width: 5rem;
  margin: auto;
}
.el-button + .el-button {
}
</style>